/**
 * 里程碑列表功能
 * 
 * @type {milestone_list}
 */
var milestone_list = function() {
    //里程碑列表操作栏图标操作
    var operation_icon=[
        {id:"milestone_issue",icon_url:"/milestone_issue.png",description:language.milestone.browser_issues},
        {id:"edit",icon_url:"/edit.gif",description:language.milestone.update},
        {id:"remove",icon_url:"/remove.gif",description:language.milestone.remove},
        {id:"finish",icon_url:"/finish.png",description:language.milestone.finish}
    ];
    var milestone_status_icon=[
        {id:"finished",icon_url:"/finished.png",description:language.milestone.finished},
        {id:"unfinished",icon_url:"/unfinished.png",description:language.milestone.unfinished}
    ];
	var grid_id = 'milestone_list';
    var milestone_finished_layer = $("#milestone_finished_layer").layer({title : language.issue.issue_colse_comment,width : "550px",height : "320px",singleton: true});
    var kindeditor=null;
    return {
		/**
		 * 显示列表
		 */
		list : function() {
			cdms.common.jart_grid(grid_id, {
				sortField : 'starttime',
				url : "list.json",
				idFields : 'id',
				queryParams :{project_id:project_id},
				gridline : 'horizontal',
				showCheckBox:false,
				columns : [ [
                    {
					title : language.milestone.column_1,
					field : "name",
					headAlign : 'left',
					width : "12%",
					sortfield : "name",
					formatter : milestone_list.name_format
				},
                    {
					title : language.milestone.column_2,
					field : "createUser",
					headAlign : 'left',
					width : "150px",
                    formatter : milestone_list.accountNameFormatter
				}, {
					title : language.milestone.column_3,
					field : "owner_name",
                    width : "150px",
					headAlign : 'left'
					
				}, {
                    title : language.milestone.column_7,
                    field : "counts",
                    headAlign : 'left',
                    formatter : milestone_list.countsFormatter
                },
                    {
					title : language.milestone.column_4,
					field : "starttime",
					headAlign : 'left',
					width : "150px",
					sortfield : "starttime"
				},
				{
					title : language.milestone.column_5,
					field : "duetime",
					headAlign : 'left',
					width : "150px",
					sortfield : "duetime"
				},{
                        title : language.milestone.column_6,
                        field : "status",
                        headAlign : "center",
                        align:"center",
                        width : "90px",
                        sortfield : "status" ,
                        formatter: milestone_list.statusFormatter
                    },
				 {
					title : language.common.operation,
					field : "operation",
					align : 'center',
					width : "220px",
					formatter : milestone_list.operationFormatter
				} ] ],
				// 选中触发事件
				onCheck : function() {
					var toolbar = $('#milestone_toolbar').toolbar();
					var datasources = toolbar.attr("dataSource")[0];
					if(datasources.length > 0){
						var rows = $('#' + grid_id).grid("checkedRows");
						toolbar.litems(1).attr('disabled',rows.length == 0);
					}
				}
			});
		},

		/**
		 * 跳转到新增
		 */
		add : function() {
			window.location.href = "edit/0/"+project_id;
		},

		/**
		 * 删除操作
		 */
		remove : function(id) {
			var ids_post = null;
			if (id == 0) {
				var rows = $('#' + grid_id).grid("checkedRows");
				if (rows.length == 0) {
					$.alert(language.issue.not_checked,
							language.note.alert_title);
					return undefined;
				}
				ids_post = cdms.common.grid_select_ids(grid_id);
			} else {
				ids_post = id;
			}
			cdms.common.confirm(language.milestone.remove_tip_msg, function() {
				cdms.common.ajax_post('delete', {
					ids : ids_post
				}, function(json) {
					var data =  eval('(' + json + ')');
					if(data.type==0){
					}
					$('#' + grid_id).grid().loadData();
				});
			});
		},
        countsFormatter :function(item){
        	if(item.cate_count!=null){
        	    return '待处理:&nbsp;&nbsp;<a href="'+js_param.issue_url+item.id+'#todo'+'">'+item.cate_count.open+'</a>&nbsp;&nbsp;&nbsp;&nbsp;已处理 :&nbsp;&nbsp;<a href="'+js_param.issue_url+item.id+'#done'+'">'+item.cate_count.closed+'</a>';		
        	} else {
                return '待处理:&nbsp;&nbsp;<a href="'+js_param.issue_url+item.id+'#todo'+'">'+"0"+'</a>&nbsp;&nbsp;&nbsp;&nbsp;已处理 :&nbsp;&nbsp;<a href="'+js_param.issue_url+item.id+'#done'+'">'+"0"+'</a>';		
        	}
        },
        name_format:function(item){
            var k="info/"+item.id;
          return '<a href="'+k+'">'+item.name+'</a>';
        },
        accountNameFormatter:function(item){
            var account;
            $.each(account_json,function(index,object){
                if(object.id == item.createUser){
                    account=object;
                    return false;
                }
            });
            return account.name;
        },
        statusFormatter:function(item)
        {
            var status={};
            var template="";
            if(item.status==true){
                template='<img src="' + js_param.image_url +milestone_status_icon[0].icon_url+'"title="'+milestone_status_icon[0].description+'"height="16" width="16" border="0"/>';
            }else{
                template='<img src="' + js_param.image_url +milestone_status_icon[1].icon_url+'"title="'+milestone_status_icon[1].description+'"height="16" width="16" border="0"/>';
            }
              return template;
        },
		/**
		 * format the hyper link of operation
		 * 
		 * @param item
		 * @param index
		 * @return {*}
		 */
		operationFormatter : function(item, index) {
			var template="";
			if(item.status==true){
				template ='<a href="'+js_param.issue_url+item.id+'#all'+'"><img src="' + js_param.image_url +operation_icon[0].icon_url+'"title="'+operation_icon[0].description+'"height="16" width="16" border="0"/></a>';
			} else if(role_json=="Admin" ||item.owner==login_json||Leader_json=="Leader"){
                template = '<a href="'+js_param.issue_url+item.id+'#all'+'"><img src="' + js_param.image_url +operation_icon[0].icon_url+'"title="'+operation_icon[0].description+'"height="16" width="16" border="0"/></a> &nbsp;&nbsp;'+
                    '<a href="edit/{0}/{5}"><img src="' + js_param.image_url +operation_icon[1].icon_url+'"title="'+operation_icon[1].description+'"height="16" width="16" border="0"/></a> &nbsp;&nbsp;'+
                    '<a href="javascript:;" onclick="milestone_list.remove({0})"><img src="' + js_param.image_url +operation_icon[2].icon_url+'"title="'+operation_icon[2].description+'"height="16" width="16" border="0"/></a> &nbsp;&nbsp;'+
                    '<a href="javascript:;" onclick="milestone_list.milestone_finished(\'{0}\',\'{6}\')"><img src="' + js_param.image_url +operation_icon[3].icon_url+'"title="'+operation_icon[3].description+'"height="16" width="16" border="0"/></a>';
            }else {
				template =  '';
			}
			return template.format(item.id, language.common.update,
					language.common.remove,language.common.show_issue,language.common.set_reach,item.project,item.name);
		},
//
//		/**
//		 * 为里程碑名添加超链接 查看详细
//		 * @param item 要格式化的行
//		 * @param index
//		 */
//		title_format : function(item,index){
//			var template='<a href="{0}/{1}/{2}">{3}</a>';
//			return template.format(js_param.view_url,item.id,project_id,item.name);
//		},
        /**
         * 设置里程碑完成
         * @param id
         */
        milestone_finished :function(id,name){
            var title =language.milestone.confirm_setting+name+"   "+language.milestone.finish ;
            $("#milestone_finished_layer").layer().attr("title",title)
            milestone_finished_layer.open();
            $("#milestone_id").attr("value",id);
            milestone_list.create_kindeditor('milestone_txt_answer');
        },
        /**
         * 设置里程碑达到
         */
        set_reach:function(){
            var form_msg = $('#close_form_msg');
            form_msg.html("<span class='ajax_processing'>" + language.issue.issue_comment_submit_note + "</span>");
            if (kindeditor.isEmpty()) {
                $.alert(language.issue.issue_comment_empty_msg, language.note.alert_title);
                return false;
            }
            form_msg.show();
            var milestone = $("#milestone_id").attr("value");
            var params = {'id_post':milestone,'content':kindeditor.html()};
            cdms.common.ajax_post('set_reach', params, function(json) {
                var json =  eval('(' + json + ')');
                    if (json.type) {
                        var checked = $("input[type=checkbox][name='build_check']").attr('checked');
                        if(checked){
                            window.location.href=js_param.head_milestone_wizard_url+milestone;
                        }else{
                            $("input[type=checkbox][name='build_check']").attr('checked','checked');
                            kindeditor.html("");
                            milestone_finished_layer.close();
                            form_msg.hide();
                        }
                    } else {
                        form_msg.html("<span class='error_msg'>" + json.msg + "</span>");
                        form_msg.show();
                    }
                    milestone_list.reload();
            });
        },
        /**
         * Close layer
         */
        close_layer          : function (id) {
            $("#" + id).layer().close();
             kindeditor.html("");
        },
        /**根据提供的  textarea  id 创建 kindeditor*/
        create_kindeditor:function(layer_id){
              if(kindeditor == null){
                kindeditor = KindEditor.create('#'+layer_id, {
                    themeType            : 'default',
                    resizeType           : 0,
                    urlType              : 'domain',
                    shadowMode           : false,
                    allowPreviewEmoticons: false,
                    allowImageUpload     : true,
                    allowFlashUpload     : false,
                    uploadJson           : js_param.upload_url,
                    afterCreate          : function () {
                        KindEditor.ctrl(this.edit.iframe.get(0).contentWindow.document, 13, function () {
                            $('#'+layer_id).parent().submit();
                        });
                    },
                    afterChange          : function () {
                        this.sync();
                    },
                    items                : ['bold', 'italic', 'underline', 'strikethrough', 'removeformat', '|', 'insertorderedlist', 'insertunorderedlist',
                        'forecolor', 'hilitecolor', 'fontname', 'fontsize', '|', 'link', 'unlink', 'image', 'flash', 'quote', '|', 'source', 'about']
                });
            }
        },
		/**
		 * 刷新表格
		 */
		reload : function() {
			$('#' + grid_id).grid().loadData();
		}
	}
}();

$(document).ready(function() {
	milestone_list.list();
});